package com.hyts.common.conn;

import com.hyts.codegen.common.base.LoadGenerator;
import com.hyts.codegen.common.base.ProcessConnection;
import com.hyts.codegen.common.model.JdbcGenModel;
import com.hyts.common.base.LoadGenerator;
import com.hyts.common.base.ProcessConnection;
import com.hyts.common.model.JdbcGenModel;
import com.hyts.dbmeta.config.DbTypeConfig;
import com.hyts.dbmeta.config.connection.MySQLConnectionConfig;
import com.hyts.dbmeta.handler.DBMetadataHandler;
import com.hyts.dbmeta.handler.mysql.MySqlConnectionHandler;
import com.hyts.dbmeta.handler.mysql.MysqlNewConnectionHandler;
import com.hyts.dbmeta.model.TableMetadata;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.List;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.codegen.process.conn
 * author:Libo/Alex
 * create-date:2019-06-18 14:16
 * email:liboware@gmail.com
 * description:此类主要用于: 构建jdbc处理器操作连接对象
 */
@Slf4j
public class JdbcProcessConnection extends ProcessConnection<JdbcGenModel> {

    /**
     * 构造器
     * @param loadGenerator
     */
    public JdbcProcessConnection(LoadGenerator loadGenerator) {
        super(loadGenerator);
    }


    @Override
    public List<TableMetadata> getTable(JdbcGenModel jdbcGenModel) {
        //构建数据库连接对象
        try {
            if(jdbcGenModel == null)
                 setJdbcGenModel(getLoadGenerator().loadConfig(jdbcGenModel));
            DbTypeConfig dbTypeConfig = DbTypeConfig.validate(getJdbcGenModel().getJdbcModel().getDbType());
            Connection connection = null;
            switch(dbTypeConfig){
                case MYSQL_OLD:
                    connection = new MySqlConnectionHandler
                            (new MySQLConnectionConfig()).getMysqlConnection(getJdbcGenModel().getJdbcModel().getIp(),
                            getJdbcGenModel().getJdbcModel().getPort(),
                            getJdbcGenModel().getJdbcModel().getUserName(),
                            getJdbcGenModel().getJdbcModel().getPassword(),
                            getJdbcGenModel().getJdbcModel().getDatabase());
                    break;
                case MYSQL_NEW:
                    connection =  new MysqlNewConnectionHandler
                            (new MySQLConnectionConfig()).getMysqlConnection(getJdbcGenModel().getJdbcModel().getIp(),
                            getJdbcGenModel().getJdbcModel().getPort(),
                            getJdbcGenModel().getJdbcModel().getUserName(),
                            getJdbcGenModel().getJdbcModel().getPassword(),
                            getJdbcGenModel().getJdbcModel().getDatabase());
                    break;
                case UNKNOWN:
                    throw new IllegalArgumentException(MessageFormat.format("传入的gen.jdbc.dbtype值有误{0}",getJdbcGenModel().getJdbcModel().getDbType()));
            }
            return DBMetadataHandler.builder().build().getTableMetadata(connection);
        } catch (SQLException e) {
            log.error("数据库连接构建处理器失败!",e);
        }
        return null;
    }

}
